import remove_1 from '@dfeidao/atom-web/file/remove';
import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import remove from '@dfeidao/atom-web/local/remove';
import fire from '@dfeidao/atom-web/msg/fire';
import nodejs from "@dfeidao/atom-web/msg/nodejs";
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import s_get from '@dfeidao/atom-web/storage/get';
import { spaceid } from '../../atom/config';
// import whoami from '@dfeidao/atom-web/msg/whoami';
interface Aa {
	filename: string;
	metadata: {
		originialFileName: string;
	};
	contentType: string;
}


export default async function a002(fd: IFeidaoAiBrowserComponent) {
	//  修改
	// const user = await whoami(get_user_info);
	// console.log(user)
	const prid = await s_get('productid');
	const _id = get(fd, 'id');
	const usercode = fd.data.node.querySelector<HTMLInputElement>('#usercode').value;
	const user_name = fd.data.node.querySelector<HTMLInputElement>('#user_name').value;
	const name_spell = fd.data.node.querySelector<HTMLInputElement>('#name_spell').value;
	const is_disabled = fd.data.node.querySelector<HTMLInputElement>('#is_disabled').value === 'true' ? true : false;
	const phone = fd.data.node.querySelector<HTMLInputElement>('#phone').value;
	// const a = fd.data.node.querySelector<HTMLInputElement>('#avatar_path');
	// const wktp = a.getAttribute('data-feidao-w');

	if (!usercode) {
		error('用户名不能为空');
		return;
	}
	if (!user_name) {
		error('昵称不能为空');
		return;
	}
	let avatar_path;
	let avatar_name;
	let avatar_type;
	const head = get(fd, 'res_upload') as Aa;
	// console.log(head)
	if (!head) {

	} else {
		avatar_path = head.filename;
		avatar_name = head.metadata.originialFileName;
		const contentType = head.contentType;
		const index = contentType.lastIndexOf("/");
		avatar_type = contentType.substring(index + 1, contentType.length);
	}
	const oldcode = get(fd, 'oldcode');
	if (oldcode !== usercode) {
		const query1 = {
			filter: {
				usercode
			},
			productid: prid,
			repeat: 'repeat'
		};
		const res1 = await nodejs<{ dataArray: object[] }>('consumer/zj-000003/consumer-find', query1);
		if (res1.dataArray.length !== 0) {
			error('用户名已存在');
			return;
		}
	}
	const picture = get(fd, 'beau') as string[];
	if (picture && picture.length) {
		if (avatar_path !== picture[0]) {
			await remove_1(spaceid, picture);
		}
	}
	const query = {
		_id,
		usercode,
		user_name,
		name_spell,
		is_disabled,
		phone,
		avatar_path,
		avatar_name,
		avatar_type,
		productid: prid
	};

	const res = await nodejs('consumer/zj-000003/consumer-upd1', query);
	if (res === 'success') {

		fire('zj-000001', 'ec-003');
		const node1 = fd.data.node.querySelector<HTMLDivElement>('[data-feidao-zj="zj"]');
		node1.classList.add('display-n');
		remove(fd, 'id');
		success('修改成功');
	} else {

		error('修改失败');
	}
}
